package com.tanhua.dubbo.api.impl;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public long save(Comment comment) throws NoSuchFieldException, IllegalAccessException {
        mongoTemplate.insert(comment);
        Query query = new Query(Criteria.where("_id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc(comment.getCol(),1);
        mongoTemplate.updateFirst(query,update, Publish.class);
        Publish pub = mongoTemplate.findById(comment.getPublishId(), Publish.class);
        String colType = comment.getCol();
        if(pub != null){
            Field field = pub.getClass().getDeclaredField(colType);
            field.setAccessible(true);
            return (Integer)field.get(pub);
        }else{
            return 0;
        }

    }

    @Override
    public long delete(Comment comment) throws NoSuchFieldException, IllegalAccessException {

        Query query = new Query(Criteria.where("_id").is(comment.getPublishId())
        .and("commentType").is(comment.getCommentType()).and("pubType")
                .is(comment.getPubType()).and("userId").is(comment.getUserId()));

        mongoTemplate.remove(query,Comment.class);

        Query q = new Query(Criteria.where("_id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc(comment.getCol(),-1);
        mongoTemplate.updateFirst(q,update,Publish.class);

        Publish pub = mongoTemplate.findById(comment.getPublishId(), Publish.class);
        String col = comment.getCol();
        if(pub != null){
            Field field = pub.getClass().getDeclaredField(col);
            field.setAccessible(true);
            return (Integer)field.get(pub);
        }else{
            return 0;
        }
    }

    @Override
    public PageResult queryByMid(String movementId, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(2));
        query.with(Sort.by(Sort.Order.desc("created")));
        query.limit(pagesize).skip((page - 1)*pagesize);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageResult(page,pagesize,(int)count,comments);
    }

    @Override
    public void commitComment(Comment c) {
        mongoTemplate.insert(c);
    }

    @Override
    public PageResult queryByUser(Integer page, Integer pagesize, Long userId,Integer contentType) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<Publish> publishes = mongoTemplate.find(query, Publish.class);
        ArrayList<ObjectId> publishId = new ArrayList<>();
        if(publishes != null){
            for (Publish publish : publishes) {
                publishId.add(publish.getId());
            }
        }
        Query q = new Query(Criteria.where("publishId").in(publishId).and("commentType").is(contentType));
        q.with(Sort.by(Sort.Order.desc("created")));
        q.limit(pagesize).skip((page - 1)*pagesize);
        List<Comment> comments = mongoTemplate.find(q, Comment.class);
        long count = mongoTemplate.count(q, Comment.class);
        return new PageResult(page,pagesize,(int)count,comments);
    }

    @Override
    public PageResult queryByPID(Integer page, Integer pagesize, String pid) {
        Query query = new Query(Criteria.where("_id").is(new ObjectId(pid)));
        List<Publish> publishes = mongoTemplate.find(query, Publish.class);
        ArrayList<ObjectId> publishId = new ArrayList<>();
        if(publishes != null){
            for (Publish publish : publishes) {
                publishId.add(publish.getId());
            }
        }
        Query q = new Query(Criteria.where("publishId").in(publishId).and("commentType").is(2));
        q.with(Sort.by(Sort.Order.desc("created")));
        q.limit(pagesize).skip((page - 1)*pagesize);
        List<Comment> comments = mongoTemplate.find(q, Comment.class);
        long count = mongoTemplate.count(q, Comment.class);
        return new PageResult(page,pagesize,(int)count,comments);
    }
}

